package cn.u313.lymusic.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import cn.u313.lymusic.databinding.ItemSearchBinding
import cn.u313.lymusic.model.SearchModel
import cn.u313.lymusic.model.SearchModel.Data
import cn.u313.lymusic.utils.click
import cn.u313.lymusic.utils.dp2px
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions

class SearchItemAdapter(context: Context,val data:MutableList<SearchModel.Data>) :
    BaseRecyclerViewAdapter<SearchModel.Data, SearchItemAdapter.ViewHolder>(context,data) {
    var listener:Listener? = null
    inner class ViewHolder(val vb: ItemSearchBinding) : RecyclerView.ViewHolder(vb.root)

    override fun onBindDataForItem(
        viewHolder: ViewHolder,
        position: Int,
        mData: MutableList<SearchModel.Data>
    ) {
        val data = mData.getOrNull(position)?:return
        val vb = viewHolder.vb
        vb.ivTitle.text = data.song
        vb.tvDesc.text = "${data.singger} - ${data.album}"

        Glide.with(getMContext())
            .load(data.pic)
            .apply(RequestOptions.bitmapTransform(RoundedCorners(dp2px(getMContext(),25))))  // 设置圆角半径为30
            .into(vb.ivHead)
        vb.tvDownload.click {
            listener?.clickItem(data)
        }
    }

    override fun onCreateVH(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemSearchBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }
    interface Listener{
        fun clickItem(data: Data)
    }
}